//
//  SWTodayMusicListView.swift
//  ChildStory
//
//  Created by linhan on 2016/12/9.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit


fileprivate let ListViewTitleHeight:CGFloat = 50
fileprivate let ListViewCellHeight:CGFloat = 60
fileprivate let LeftMargin:CGFloat = 15
fileprivate let RightMargin:CGFloat = 15

class SWTodayMusicsView:UIView
{
    weak var delegate:ClickActionDelegate?
    
    var musics:[SWMusic] = []{
        didSet{
            updateView()
        }
    }
    
    private var _listView:UIView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    class func getHeight(musics:[SWMusic])->CGFloat
    {
        let count = CGFloat(musics.count)
        let height:CGFloat = count * ListViewCellHeight
        return ListViewTitleHeight + height
    }
    
    private func setup()
    {
        backgroundColor = UIColor.white
        
        let titleView:UIView = UIView()
        titleView.frame = CGRectMake(0, 0, width, ListViewTitleHeight)
        addSubview(titleView)
        
        let titleLabel = UILabel()
        titleLabel.text = "今日音乐"
        titleLabel.textColor = UIColor(hex: 0x303332)
        titleLabel.font = UIFont.systemFont(ofSize: 18)
        titleLabel.sizeToFit()
        titleLabel.frame = CGRectMake(LeftMargin, (titleView.height - titleLabel.height) * 0.5, titleLabel.width, titleLabel.height)
        titleView.addSubview(titleLabel)
        
        //更多
        let moreTapGesture = UITapGestureRecognizer(target: self, action: #selector(moreBtnTapped))
        let moreView = UIView()
        moreView.height = titleView.height
        moreView.addGestureRecognizer(moreTapGesture)
        titleView.addSubview(moreView)
        
        let moreLabel:UILabel = UILabel()
        moreLabel.text = "更多"
        moreLabel.textColor = UIColor(hex: 0xababab)
        moreLabel.font = UIFont.systemFont(ofSize: 14)
        moreLabel.sizeToFit()
        moreLabel.center = CGPointMake(moreLabel.width * 0.5, moreView.height * 0.5)
        moreView.addSubview(moreLabel)
        
        let arrow:UIImageView = UIImageView(image: UIImage(named:"index_img_arrow_bgw"))
        arrow.center = CGPointMake(moreLabel.right + 9 + arrow.width * 0.5, moreView.height * 0.5)
        moreView.addSubview(arrow)
        moreView.width = arrow.right
        moreView.x = width - RightMargin - moreView.width
        
        
        
        let line:UIView = UIView()
        line.backgroundColor = UIColor(hex: 0xdfe2e1)
        line.frame = CGRectMake(0, titleView.height - 1, width, 1)
        titleView.addSubview(line)
        
        _listView.frame = CGRectMake(0, titleView.bottom, width, 0)
        addSubview(_listView)
    }
    
    private func updateView()
    {
        _listView.removeAllSubview()
        
        let CellHeight:CGFloat = ListViewCellHeight
        var lastY:CGFloat = 0
        for (i,music) in musics.enumerated()
        {
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cellTapped(_:)))
            let cellY:CGFloat = CellHeight * CGFloat(i)
            let cell = SWMusicViewCell(frame: CGRectMake(0, cellY, width, CellHeight))
            cell.tag = i
            cell.music = music
            cell.addGestureRecognizer(tapGesture)
            _listView.addSubview(cell)
            lastY = cell.bottom
            
            if i > 0
            {
                let line:UIView = UIView()
                line.backgroundColor = UIColor(hex: 0xdfe2e1)
                line.frame = CGRectMake(LeftMargin, cellY, width - LeftMargin - RightMargin, 1)
                _listView.addSubview(line)
            }
        }
        _listView.height = lastY
        height = _listView.bottom
    }
    
    @objc private func cellTapped(_ gesture:UITapGestureRecognizer)
    {
        if let index = gesture.view?.tag
        {
            delegate?.view(self, click: CallbackAction.Tap, argument: [SWKey.index:index])
        }
    }
    
    @objc private func moreBtnTapped()
    {
        delegate?.view(self, click: CallbackAction.More, argument: nil)
    }
    
}

fileprivate class SWMusicViewCell:UIView
{
    var music:SWMusic?{
        didSet{
            updateView()
        }
    }
    
    private var _titleLabel:UILabel = UILabel()
    private var _durationLabel:UILabel = UILabel()
    private var _artistLabel:UILabel = UILabel()
    private var _imageView:ImageLoader = ImageLoader()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        backgroundColor = UIColor.white
        
        _imageView.frame = CGRectMake(LeftMargin, 11, 38, 38)
        _imageView.options.contentMode = .scaleAspectFill
        addSubview(_imageView)
        
        let durationLabelWidth:CGFloat = 50
        let durationLabelX:CGFloat = width - RightMargin - durationLabelWidth
        _durationLabel.font = UIFont.systemFont(ofSize: 12)
        _durationLabel.textAlignment = .right
        _durationLabel.textColor = UIColor(hex: 0x2f3231)
        _durationLabel.frame = CGRectMake(durationLabelX, 9, durationLabelWidth, 16)
        addSubview(_durationLabel)
        
        let titleLabelX:CGFloat = _imageView.right + 23
        _titleLabel.font = UIFont.systemFont(ofSize: 17)
        _titleLabel.textColor = UIColor(hex: 0x2f3231)
        _titleLabel.frame = CGRectMake(titleLabelX, 9, durationLabelX - titleLabelX, 21)
        addSubview(_titleLabel)
        
        _artistLabel.font = UIFont.systemFont(ofSize: 12)
        _artistLabel.textColor = UIColor(hex: 0xd8d8d8)
        _artistLabel.frame = CGRectMake(titleLabelX, _titleLabel.bottom + 7, _titleLabel.width, 16)
        addSubview(_artistLabel)
    }
    
    private func updateView()
    {
        if let music = music
        {
            _titleLabel.text = music.name
            _durationLabel.text = music.durationString
            _artistLabel.text = music.artist
            _imageView.load(music.thumbURL ?? "")
        }
    }
}


